package JET.physics;

import JET.geom.Rect2d;
import JET.geom.Vec2d;

/**
 *
 * @author 057Ry
 */
public class BoundingBox extends BoundingArea
{
    private double minX, minY, maxX, maxY;

    public void setMinX(double x)
    {
        this.minX = x;
    }
    public void setMinY(double y)
    {
        this.minY = y;
    }
    public void setMaxX(double x)
    {
        this.maxX = x;
    }
    public void setMaxY(double y)
    {
        this.maxY = y;
    }

    @Override
    public double getWidth() {
        return maxX - minX;
    }

    @Override
    public double getHeight() {
        return maxY - minY;
    }

    @Override
    public double getMinX() {
        return minX;
    }

    @Override
    public double getMinY() {
        return minY;
    }

    @Override
    public double getMaxX() {
        return maxX;
    }

    @Override
    public double getMaxY() {
        return maxY;
    }
    @Override
    public double getMaxDimension()
    {
        double width = getWidth();
        double height = getHeight();

        if ( width >= height )
            return width;
        else
            return height;
    }


    public Rect2d getRect2d()
    {
        return new Rect2d(minX, minY, maxX, maxY);
    }
    public Vec2d getV1()
    {
        return new Vec2d(minX, minY);
    }
    public Vec2d getV2()
    {
        return new Vec2d(maxX, maxY);
    }
    public Vec2d getLeftUpper()
    {
        return new Vec2d(minX, maxY);
    }
    public Vec2d getRightLower()
    {
        return new Vec2d(maxX, minY);
    }

    @Override
    public boolean intersectsWith(BoundingArea ba) 
    {
        if ( ba instanceof BoundingBox)
        {
            BoundingBox bb = (BoundingBox)ba;
            Rect2d thisRect = this.getRect2d();
            Rect2d bbRect = bb.getRect2d();

            if ( thisRect.contains( bb.getV1() ) || thisRect.contains( bb.getV2() ) ||
                 thisRect.contains( bb.getLeftUpper() ) || thisRect.contains( bb.getRightLower() ) ||
                 bbRect.contains( this.getV1() ) || bbRect.contains( this.getV2() ) ||
                 bbRect.contains( this.getLeftUpper() ) || bbRect.contains( this.getRightLower() ) )
                return true;
            else
                return false;
        }
        
        return false;
    }
    public boolean contains(Vec2d vec)
    {
        if ( vec.getX() >= minX && vec.getX() <= maxX && vec.getY() >= minY && vec.getY() <= maxY )
            return true;
        return false;
    }

    public void extendToContain(Vec2d v) {
        if (getMaxX()<v.getX()) setMaxX(v.getX());
        if (getMaxY()<v.getY()) setMaxY(v.getY());
        if (getMinX()>v.getX()) setMinX(v.getX());
        if (getMinY()>v.getY()) setMinY(v.getY());
    }
}
